library(kernlab)
library(caret)
library(ROCR)

setwd("D:/ClassificationR")

#variables
a_accuracy = 0
a_sensitivity = 0
a_specificity = 0
a_auc = 0

for(i in 1:5) {
  #membaca data training & data testing
  data.training = read.csv(paste0("iris2class.train.",i,".csv"))
  data.test = read.csv(paste0("iris2class.test.",i,".csv"))
  
  #membuat model
  model = ksvm(Species~., data.training)
  
  #melakukan prediksi 
  predict_result = predict(model, data.test[,-5])
  
  #menghitung kinerja
  performance.value = confusionMatrix(predict_result, data.test[,5])
  
  roc.prediction = prediction(as.numeric(as.factor(predict_result)), as.numeric(as.factor(data.test[,5])))
  roc.tpr.fpr = performance(roc.prediction,"tpr","fpr")
  roc.auc = performance(roc.prediction,"auc")
  
  #menggambar ROC
  if(i > 1) {
    plot(roc.tpr.fpr, add = TRUE, col="red",lty=3)
  } else {
    plot(roc.tpr.fpr, col="red",lty=3)
    abline(a=0, b= 1)
  }
  
  #menghitung jumlah setiap nilai kinerja
  a_accuracy = a_accuracy + performance.value$overall[1]
  a_sensitivity = a_sensitivity + performance.value$byClass[1]
  a_specificity = a_specificity + performance.value$byClass[2]
  a_auc = a_auc + as.numeric(roc.auc@y.values)
}

print(paste("average accuracy:", a_accuracy/i))
print(paste("average sensitivity:", a_sensitivity/i))
print(paste("average specificity:", a_specificity/i))
print(paste("average AUC:", a_auc/i))
